#!/bin/sh

# disable selinux
setenforce 0

# Remove old softwares
yum -y remove php mysql httpd;

# download dir
mkdir -p /kloxo;
cd /kloxo;

# Install kloxo
wget http://download.lxcenter.org/download/kloxo/production/kloxo-installer.sh;
sh ./kloxo-installer.sh --type=master;

# Install the Chinese language package
cd /usr/local/lxlabs/kloxo/httpdocs/lang;
wget http://ceshilnmp.googlecode.com/svn/trunk/kloxo/cn.zip;
unzip cn.zip; rm -rf cn.zip

# Install the php-mbstring for phpMyAdmin and other php scripts
yum -y install php-mbstring php-bcmath php-mhash
echo 'extension=mbstring.so' >> /usr/local/lxlabs/ext/php/etc/php.ini

# Configure some services for security eg. lx,ftp,mail
cd /kloxo;
wget http://ceshilnmp.googlecode.com/svn/trunk/kloxo/ftp_mail.sql;
mysql -uroot kloxo< /kloxo/ftp_mail.sql;
sudo chkconfig xinetd on

# optimization mysql
cd /kloxo
wget http://ceshilnmp.googlecode.com/svn/trunk/kloxo/mycnf.zip;
unzip mycnf.zip;
cp -a /kloxo/my.cnf /etc/my.cnf
service mysqld restart

# Restart kloxo
service kloxo restart;

# Stop not necessary servces to release memory and avoid email spam:
service named stop;
service qmail stop;
service courier-imap stop;

chkconfig named off;
chkconfig qmail off;
chkconfig courier-imap off;

# optimization  lighttpd
echo "server.max-worker = 4" >> /etc/lighttpd/lighttpd.conf
echo "server.max-connections = 1024" >> /etc/lighttpd/lighttpd.conf
echo "server.max-fds = 2048" >> /etc/lighttpd/lighttpd.conf
echo "server.max-keep-alive-idle = 5" >> /etc/lighttpd/lighttpd.conf
echo "server.max-keep-alive-requests = 1000" >> /etc/lighttpd/lighttpd.conf

# Install Automatically clear the log
cat >>/etc/cron.daily/delete_logs.sh<<EOF
# !/bin/bash
rm -rf /home/admin/_processed_stats/*
rm -rf /home/kloxo/httpd/lighttpd/*
rm -rf /var/log/kloxo/*
rm -f /home/httpd/*/stats/*
EOF
chmod 755 /etc/cron.daily/delete_logs.sh

# Install unrar 
wget  http://dag.wieers.com/rpm/packages/unrar/unrar-3.6.8-1.el5.rf.i386.rpm;
rpm -Uvh unrar-3.6.8-1.el5.rf.i386.rpm;

#kloxo ftp error
rm -rf /etc/xinetd.d/pureftp
wget http://ceshilnmp.googlecode.com/svn/trunk/kloxo/pureftp -P /etc/xinetd.d/

#kloxo utf-8 error
sed -i 's/AddDefaultCharset UTF-8/#AddDefaultCharset UTF-8/g' /etc/httpd/conf/httpd.conf

# Clear installation files
rm -rf /root/kloxo.sh
rm -rf /kloxo
rm -rf /home/kloxo/httpd/installsoft

YOURIP=`hostname -i`;

echo "======= All is OK. Plese login! ======= ";
echo '';
echo '*********************************************************';
echo '****                                                 ****';
echo '****           +++++ LOGIN DETAILS +++++             ****';
echo '****                                                 ****';
echo '****    ## How to login to Kloxo control panel ##    ****';
echo '****                                                 ****';
echo "****           http://${YOURIP}:7778/            ****";
echo '****                                                 ****';
echo '****     (Username: admin  Password: admin)          ****';
echo '****                                                 ****';
echo '****                                                 ****';
echo '*********************************************************';
echo '';
#restart:/script/restart
reboot